package top.jfunc.network.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.jfunc.common.http.smart.SmartHttpClient;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xiongshiyan at 2019/5/7 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@RestController
@RequestMapping("/get")
public class GetController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(GetController.class);
    @Autowired
    private SmartHttpClient smartHttpClient;

    @GetMapping("httpclient")
    public String httpclient() throws Exception{
        return smartHttpClient.get("get/query");
    }

    @GetMapping("query")
    public String getQuery(HttpServletRequest httpServletRequest){
        printQueryString(logger , httpServletRequest);

        printParams(logger , httpServletRequest);

        printHeaders(logger , httpServletRequest);

        return "success";
    }
    @GetMapping("json")
    public String getJson(HttpServletRequest httpServletRequest){
        printQueryString(logger , httpServletRequest);

        printParams(logger , httpServletRequest);

        printHeaders(logger , httpServletRequest);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("now", System.currentTimeMillis());
        return jsonObject.toString();
    }

    @GetMapping("ex")
    public String exLocal() throws Exception{
        throw new RuntimeException("服务端异常测试");
    }
}
